import request from '@/utils/request'
import { defineStore } from 'pinia'
import {
  ApiRes,
  BannerItem,
  homeNewItem,
  HomeHotItem,
  HomeBrandItem,
  HomeGoodsItem,
  homeSpecialItem
} from '@/types/data'
export default defineStore('home', {
  state () {
    return {
      bannerList: [] as BannerItem[],
      homeNewsList: [] as homeNewItem[], // 新鲜好物
      homeHotList: [] as HomeHotItem[], // 人气推荐
      homeBrandList: [] as HomeBrandItem[], // 品牌数据
      homeGoodsList: [] as HomeGoodsItem[], // 商品区块
      homeSpecialList: [] as homeSpecialItem[] // 专题
    }
  },
  actions: {
    // 轮播图获取
    async homeBanner () {
      if (this.bannerList.length) {
        return
      }
      const res = await request.request<ApiRes<BannerItem[]>>({
        url: '/home/banner'
      })
      this.bannerList = res.data.result
      console.log('轮播图数据', res)
    },
    // 获取新鲜好物
    async homeNew () {
      const res = await request.request<ApiRes<homeNewItem[]>>({
        url: '/home/new'
      })
      this.homeNewsList = res.data.result
      console.log('新鲜好物', res)
    },
    // 获取人气推荐数据
    async homeHot () {
      const res = await request.request<ApiRes<HomeHotItem[]>>({
        url: '/home/hot'
      })
      this.homeHotList = res.data.result
      console.log('人气推荐', res)
    },
    // 获取品牌数据
    async homeBrand () {
      const res = await request.request<ApiRes<HomeBrandItem[]>>({
        url: '/home/brand'
      })
      this.homeBrandList = res.data.result
      console.log('品牌', res)
    },
    // 商品区块数据获取
    async homeGoods () {
      const res = await request.request<ApiRes<HomeGoodsItem[]>>({
        url: '/home/goods'
      })
      this.homeGoodsList = res.data.result
      console.log('商品区块数据获取', res)
    },
    // 专题
    async homeSpecial () {
      const res = await request.request<ApiRes<homeSpecialItem[]>>({
        url: '/home/special'
      })
      this.homeSpecialList = res.data.result
      console.log('专题', res)
    }
  }
})
